<template>
  <div class="app-container">
    <Find @clickNumber="clickNumber1" :OriginalFilter="{ static: true }" :formList="formList" @Launchform="Launchform3" @findClicked="findData" />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <div style="display: flex">
        <div style="width: 100%">
          <Tables
            :summaryData="summaryData"
            summary
            @modifTable6="modifTable6"
            @modifTable5="modifTable5"
            @modifTable3="modifTable3"
            :isLoading="isLoading"
            :functionList6="true"
            :tableHeader="tableHeader"
            :tableData="BlankingTableDate"
            :TableHeight="100"
          />
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </div>
      </div>
    </el-card>

    <el-dialog :title="modifTable3String" v-model="dialogVisible6" width="70%" class="dialog pb0">
      <Find @findClicked="findData2" @Launchform="Launchform" style="margin-bottom: 20px" :OriginalFilter="{ static: true }" :formList="formList2" />

      <Tables
        @deleteRow="deleteRow"
        :deleteButton="!active"
        :isLoading="isLoading1"
        :functionList="true"
        :tableHeader="tableHeader1"
        :tableData="mainQueryTableDate"
        :TableHeight="100"
      />
      <PageModule :PageData="{ total: total1, buttonS: true }" @pageSizeChange="handleSizeChange1" @pageChange="handleCurrentChange1" />
    </el-dialog>

    <el-dialog :title="modifTable5String" v-model="dialogVisible7" width="30%" class="dialog pb0">
      <el-form :model="form" label-width="auto" style="max-width: 100%">
        <el-form-item :label="$t(`Print.Outbounddate`)">
          <el-date-picker v-model="form.collectTime" type="date" placeholder="" style="width: 120px" format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.Matching`) }}</span>
          <el-select @change="selectChange3" v-model="form.groupNo" placeholder="" style="width: 100px">
            <el-option v-for="item in TransceiverTypeParms123" :key="item.optionKey" :label="item.optionLabel" :value="item.optionValue" />
          </el-select>
        </el-form-item>

        <el-form-item label="PO:">
          <el-input v-model="form.poNo" style="width: 100px" placeholder="" />

          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.even`) }}</span>
          <el-input-number :max="listNumberMin" v-model="form.pairQty" :min="1" />
        </el-form-item>

        <el-form-item :label="$t(`Print.RemainingEvens`)">
          <el-input disabled v-model="form.remainQty" style="width: 100px" placeholder="" />

          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.InvoiceNumber`) }}</span>
          <el-input v-model="form.invoiceNo" style="width: 150px" placeholder="" />
        </el-form-item>

        <el-form-item :label="$t(`Print.CabinetNumber`)">
          <el-input v-model="form.containerNo" style="width: 150px" placeholder="" />
        </el-form-item>

        <el-form-item :label="'海关编码'">
          <el-input v-model="rowParms.outHSCode" style="width: 150px" placeholder="" />
        </el-form-item>
      </el-form>

      <div>
        <el-button type="primary" size="default" @click="AddList">{{ $t(`Print.Add`) }}</el-button>
        <el-button type="danger" size="default" @click="deleteRows">{{ $t(`Print.Delete`) }}</el-button>
        <Table
          @currentRow="clicktogetdata"
          @inputChange="checkboxChange2"
          :tableHeader="tableHeader2"
          :tableData="mainQueryTableDate1"
          :TableHeight="50"
        />
        <el-button type="primary" size="default" @click="AddListMethods">{{ $t(`Print.OutboundShipments`) }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="modifTable5String" v-model="dialogVisible8" width="30%" class="dialog pb0">
      <el-form :model="form1" label-width="auto" style="max-width: 100%">
        <el-form-item :label="$t(`Print.TheDateOfDeposit`)">
          <el-date-picker v-model="form1.collectTime" type="date" placeholder="" style="width: 120px" format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.SurrenderUnit`) }}</span>
          <el-select @change="selectChange3" v-model="form1.deptNo" placeholder="" style="width: 100px">
            <el-option v-for="item in formList2[3].selectOptions" :key="item.optionKey" :label="item.optionLabel" :value="item.optionValue" />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t(`Print.Matching`)">
          <el-select @change="selectChange3" v-model="form1.groupNo" placeholder="" style="width: 100px">
            <el-option v-for="item in TransceiverTypeParms123" :key="item.optionKey" :label="item.optionLabel" :value="item.optionValue" />
          </el-select>

          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.Break`) }}</span>
          <el-input v-model="form1.poNo" style="width: 100px" placeholder="" />
        </el-form-item>

        <el-form-item :label="$t(`Print.even`)">
          <el-input-number :max="listNumberMax" v-model="form1.pairQty" :min="1" />

          <span style="margin-left: 40px; margin-right: 15px">{{ $t(`Print.RemainingEvens`) }}</span>
          <el-input disabled v-model="form1.remainQty" style="width: 100px" placeholder="" />
        </el-form-item>
        <el-form-item :label="'海关编码'">
          <el-input v-model="rowParms.inHSCode" style="width: 150px" placeholder="" />
        </el-form-item>
      </el-form>

      <div>
        <el-button type="primary" size="default" @click="AddList2">{{ $t(`Print.Add`) }}</el-button>
        <el-button type="danger" size="default" @click="deleteRows3">{{ $t(`Print.Delete`) }}</el-button>
        <Table
          @currentRow="clicktogetdata2"
          @inputChange="checkboxChange23"
          :tableHeader="tableHeader23"
          :tableData="mainQueryTableDate12"
          :TableHeight="50"
        />
        <el-button type="primary" size="default" @click="AddListMethods3">{{ $t(`Print.SurrenderToTheTreasury`) }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
import fileRequest from '@/utils/fileRequest'
export default defineComponent({
  name: 'FinishedProductSendingAndReceiving'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import {
  SubmitTheOutgoingJob3,
  SubmitTheOutgoingJob,
  mainQuery,
  Blanking,
  TransceiverType,
  DetailDeletion,
  dateQuery,
  TheCommandOutput,
  Blanking1,
  BulkReceiving,
  BatchQuery,
  deptNameQuery,
  GetPackingMethod
} from '@/api/WarehouseDepartment/WebPages/FinishedProductSendingAndReceiving'
import { ElMessage, ElMessageBox } from 'element-plus'

const active = ref(true)

const clearFormList = () => {
  formList.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

// 日期
const formatCurrentDate = (date?: any) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  let newMonth = month > 10 ? month : '0' + month
  let newDay = day > 10 ? day : '0' + day
  return `${year}-${newMonth}-${newDay}`
}

const currentDate = ref(new Date())

// 缴库
const form1 = ref<any>({
  collectTime: formatCurrentDate(currentDate.value),
  poNo: '',
  groupNo: '',
  containerNo: '',
  invoiceNo: '',
  lotNo: '',
  pairQty: '',
  remainQty: '',
  packTypes: [],
  deptNo: ''
})

const findData2 = (row: any) => {
  if (row === '置空') {
    clearFormList3()
  }
}

const clearFormList3 = () => {
  formList2.value.forEach((item: any) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
    if (item.type === 'select') {
      item.selectValue = ''
    }
  })
}

const summaryData = ref([
  {
    index: 9,
    num: 0
  }
])

const AddListMethods3 = () => {
  SubmitTheOutgoingJobMehtods3()
}

// 提交出库作业
const SubmitTheOutgoingJobMehtods3 = async () => {
  // form1.value.groupNo = form.value.groupNo
  form1.value.deptNo = form1.value.deptNo.split(' ')[1]
  form1.value.customsCode = rowParms.value.inHSCode

  // form.value.packTypes = mainQueryTableDate1.value.map((item:any) => {
  //   item.packType = item.title.selectValue.split(' ')[0]
  //   item.caseQty = item.inLotNum
  //   delete item.title
  //   delete item.inLotNum
  //   delete item.outLotNum
  //   delete item.change
  //   delete item.disableConfirm
  //   delete item.id
  //   return item
  // })
  form1.value.packTypes = mainQueryTableDate12.value.map((item: any) => {
    // 从原始对象中提取需要的属性，并创建一个新的对象
    return {
      packType: item.title.selectValue.split(' ')[0], // 提取 packType
      caseQty: item.inLotNum // 提取 caseQty
      // 这里可以继续提取需要的其他属性
    }
  })
  try {
    const result = await SubmitTheOutgoingJob3(form1.value)
    if (result.code === 200) {
      dialogVisible8.value = false
      BlankingMethods()
      mainQueryTableDate12.value = []
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('出库错误')
  }
}

// 删除选定行的函数
const deleteRows3 = () => {
  // 检查是否选定了行
  if (clicktogetdataRow.value) {
    // 从数据列表中删除选定的行
    const index = mainQueryTableDate12.value.findIndex((item: any) => item.id === clicktogetdataRow.value!.id)
    if (index !== -1) {
      mainQueryTableDate12.value.splice(index, 1) // 删除选定行
      clicktogetdataRow.value = null
    }

    // 清空选定的行
  } else {
    JMessage.warning('请选择需要删除的数据')
  }
}

// 选择数据行的函数
const clicktogetdata2 = (row: any) => {
  clicktogetdataRow.value = row
}

const checkboxChange23 = (row: any) => {
  if (row.inLotNum !== '') {
    let num = Number(row.title.selectValue.split(' ')[1])
    row.outLotNum = Number(row.inLotNum) * num
  } else {
    row.outLotNum = ''
  }
  form1.value.pairQty = mainQueryTableDate12.value.reduce((total: any, item: any) => {
    // 确保 outLotNum 存在且为数字，累加到 total
    return total + (item.outLotNum || 0) // 使用逻辑或运算符处理 undefined 或 null
  }, 0) // 初始值设置为 0
}

const mainQueryTableDate12 = ref<any>([])

const AddList2 = () => {
  mainQueryTableDate12.value.push({
    title: {
      selectValue: '',
      options: GetPackingMethodsTableDate
    },
    change: 'Y',
    disableConfirm: true,
    inLotNum: '',
    outLotNum: '',
    id: Date.now()
  })
}

// 删除
// 用于存储被选择的数据行
const clicktogetdataRow = ref<any>(null)

// 选择数据行的函数
const clicktogetdata = (row: any) => {
  clicktogetdataRow.value = row
}

// 删除选定行的函数
const deleteRows = () => {
  // 检查是否选定了行
  if (clicktogetdataRow.value) {
    // 从数据列表中删除选定的行
    const index = mainQueryTableDate1.value.findIndex((item: any) => item.id === clicktogetdataRow.value!.id)
    if (index !== -1) {
      mainQueryTableDate1.value.splice(index, 1) // 删除选定行
      clicktogetdataRow.value = null
    }

    // 清空选定的行
  } else {
    JMessage.warning('请选择需要删除的数据')
  }
}

const form = ref<any>({
  collectTime: formatCurrentDate(currentDate.value),
  poNo: '',
  groupNo: '',
  containerNo: '',
  invoiceNo: '',
  lotNo: '',
  pairQty: '',
  remainQty: '',
  packTypes: [],
  customsCode: ''
})

const selectChange3 = () => {
  form.value.remainQty = modifTable5Rows.value
}

// 提交出库作业
const SubmitTheOutgoingJobMehtods = async () => {
  form.value.groupNo = form.value.groupNo
  form.value.customsCode = rowParms.value.outHSCode

  // form.value.packTypes = mainQueryTableDate1.value.map((item:any) => {
  //   item.packType = item.title.selectValue.split(' ')[0]
  //   item.caseQty = item.inLotNum
  //   delete item.title
  //   delete item.inLotNum
  //   delete item.outLotNum
  //   delete item.change
  //   delete item.disableConfirm
  //   delete item.id
  //   return item
  // })
  form.value.packTypes = mainQueryTableDate1.value.map((item: any) => {
    // 从原始对象中提取需要的属性，并创建一个新的对象
    return {
      packType: item.title.selectValue.split(' ')[0], // 提取 packType
      caseQty: item.inLotNum // 提取 caseQty
      // 这里可以继续提取需要的其他属性
    }
  })
  try {
    const result = await SubmitTheOutgoingJob(form.value)
    if (result.code === 200) {
      dialogVisible7.value = false
      BlankingMethods()
      mainQueryTableDate1.value = []
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('出库错误')
  }
}

const checkboxChange2 = (row: any) => {
  if (row.inLotNum !== '') {
    let num = Number(row.title.selectValue.split(' ')[1])
    row.outLotNum = Number(row.inLotNum) * num
  } else {
    row.outLotNum = ''
  }
  form.value.pairQty = mainQueryTableDate1.value.reduce((total: any, item: any) => {
    // 确保 outLotNum 存在且为数字，累加到 total
    return total + (item.outLotNum || 0) // 使用逻辑或运算符处理 undefined 或 null
  }, 0) // 初始值设置为 0
}

const AddListMethods = () => {
  SubmitTheOutgoingJobMehtods()
}

const GetPackingMethodsTableDate = ref()
// 装箱方式
const GetPackingMethods = async () => {
  try {
    let NewArray = []
    const result = await GetPackingMethod()
    NewArray = result.reduce((arr: any, item: any) => {
      if (item.packType) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.packType + ' ' + item.caseQty)
      }
      return arr
    }, [])
    GetPackingMethodsTableDate.value = EvaluatesTheArrayReturnValue1(NewArray)
  } catch (error) {
    JMessage.error('装箱方式数据获取出错')
  }
}

const mainQueryTableDate1 = ref<any>([])

const AddList = () => {
  mainQueryTableDate1.value.push({
    title: {
      selectValue: '',
      options: GetPackingMethodsTableDate
    },
    change: 'Y',
    disableConfirm: true,
    inLotNum: '',
    outLotNum: '',
    id: Date.now()
  })
}

// 批次查询
const BatchQueryParms = ref({
  lotNo: '',
  lsNo: '',
  partName: '',
  sizeNo: ''
})

const batchNoListList = ref([])

const BatchQueryMethodsTableDate = ref([])

const dialogVisible9 = ref(false)

const selectList5 = (row: any) => {
  batchNoListList.value = row.map((item: any) => {
    return item.batchNo
  })
}

const clicktogetdata3 = (row: any, list: any) => {
  BatchQueryParms.value.lsNo = row.lsNo
  BatchQueryParms.value.partName = row.partName
  BatchQueryParms.value.sizeNo = row.sizeNo
  BatchQueryMethods()
  dialogVisible9.value = true
}

const BatchQueryMethods = async () => {
  try {
    const result = await BatchQuery(BatchQueryParms.value)
    BatchQueryMethodsTableDate.value = result
  } catch (error) {
    JMessage.error('批次查询失败请重视')
  }
}

// 发料
const dialogVisible8 = ref(false)

const EvaluatesTheArrayReturnValue1 = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      value: item,
      label: item
    }
  })
}
const modifTable5String2 = ref('')
const listNumberMax = ref(0)
const listNumberMin = ref(0)
const modifTable6 = (row: any) => {
  listNumberMax.value = row.row.inQty1
  collectType.value = 'I'
  modifTable5String2.value = row.row.lotNo
  form1.value.lotNo = row.row.lotNo
  modifTable5String.value = `指令【${row.row.lotNo}】成品缴库作业`
  Blanking1Parms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.type = 0
  TheCommandOutputParms.value.lotNo = row.row.lotNo
  BatchQueryParms.value.lotNo = row.row.lotNo
  TheCommandOutputParms.value.type = 0
  TransceiverTypeMethods()
  deptNameQueryMethods()
  TheCommandOutputMethods()
  // Blanking1Methods()
  dialogVisible8.value = true
  rowParms.value = row.row
}

const checkboxChange1 = (row: any) => {
  if (row.inQty < row.inQty1) {
    JMessage.warning('报数不可大于剩余数量')
    row.inQty1 = 0
  }
}

// 收料
const BulkReceivingParms = ref<any>([])

const selectList1 = (row: any) => {
  BulkReceivingParms.value = row.map((item: any) => {
    item.lotNo = Blanking1Parms.value.lotNo
    item.collectPad = 'web'
    return item
  })
}

const selectList = (row: any) => {
  Blanking1Parms.value.partNames = row.map((item: any) => {
    return item.partName
  })
  if (Blanking1Parms.value.partNames.length !== 0) {
    Blanking1Methods()
  } else {
    Blanking1Parms.value.partNames = null
    Blanking1Methods()
  }
}

const BulkReceivingMethods1 = () => {
  Avtive.value = 'H'
  BulkReceivingMethods()
}

const BulkReceivingMethods2 = () => {
  Avtive.value = 'S'
  BulkReceivingMethods()
}

const Avtive = ref('')

const BulkReceivingMethods = async () => {
  if (BulkReceivingParms.value.length === 0) {
    JMessage.warning('请勾选需要批量收料的数据')
  } else {
    BulkReceivingParms.value = BulkReceivingParms.value.map((item: any) => {
      item.type = Avtive.value
      item.inQty = item.inQty1
      item.batchNoList = batchNoListList.value
      return item
    })
    try {
      const result = await BulkReceiving(BulkReceivingParms.value)
      if (result.code === 200) {
        Blanking1Methods()
        BlankingMethods()
        JMessage.success(result.message)
      } else {
        JMessage.error(result.message)
      }
    } catch (error) {
      JMessage.error('批量收料出错请重试')
    }
  }
}

// 表格回显1
const Blanking1TableDate = ref([])

const Launchform7 = (row: any) => {
  Blanking1Parms.value.lsNos = row[1].selectValue1
  Blanking1Parms.value.sizeNos = row[0].selectValue1
  Blanking1Methods()
}

const Blanking1Parms = ref<any>({
  lotNo: '',
  lsNos: null,
  partNames: null,
  sizeNos: null,
  type: '',
  pageNum: 1,
  pageSize: 40
})

const total3 = ref(0)

const handleSizeChange2 = (val: number) => {
  Blanking1Parms.value.pageSize = val
  Blanking1Methods()
}

const handleCurrentChange2 = (val: number) => {
  Blanking1Parms.value.pageNum = val
  Blanking1Methods()
}

const isLoading8 = ref(false)

const total5 = ref(0)

const handleSizeChange5 = (val: number) => {
  Blanking1Parms.value.pageSize = val
  Blanking1Methods()
}

const handleCurrentChange5 = (val: number) => {
  Blanking1Parms.value.pageNum = val
  Blanking1Methods()
}

const Blanking1Methods = async () => {
  isLoading8.value = true
  try {
    const result = await Blanking1(Blanking1Parms.value)
    if (result.dtos.length !== 0) {
      Blanking1TableDate.value = result.dtos.map((item: any) => {
        item.inQty1 = item.inQty
        // item.change = 'Y'
        // item.disableConfirm = true
        return item
      })
    }
    total3.value = result.total
    total5.value = result.total
    isLoading8.value = false
  } catch (error) {
    isLoading8.value = false
    JMessage.error('表格查询出错')
  }
}

// 收发料左面表格数据
const TheCommandOutputParms = ref<any>({
  lotNo: '',
  lsNos: null,
  partNames: null,
  sizeNos: null,
  type: ''
})

const isLoading7 = ref(false)

const TheCommandOutputTableDate = ref<any>([])

const TheCommandOutputMethods = async () => {
  isLoading7.value = true
  try {
    const result = await TheCommandOutput(TheCommandOutputParms.value)

    TheCommandOutputTableDate.value = result.partName.map((item: any) => {
      return { partName: item }
    })

    formList5.value[0].selectOptions = EvaluatesTheArrayReturnValue(result.sizeNo)
    formList5.value[1].selectOptions = EvaluatesTheArrayReturnValue(result.lsNo)

    isLoading7.value = false
  } catch (error) {
    isLoading7.value = false
    JMessage.error('部位数据查询出错')
  }
}

const dialogVisible7 = ref(false)

const modifTable5String = ref('')

const modifTable5Rows = ref('')

const modifTable5 = (row: any) => {
  modifTable5String2.value = row.row.lotNo
  collectType.value = 'O'
  modifTable5String.value = `指令【${row.row.lotNo}】成品出库作业`
  listNumberMin.value = row.row.outQty1
  form.value.lotNo = row.row.lotNo
  mainQueryParms.value.lotNo = row.row.lotNo

  modifTable5Rows.value = row.row.outQty1

  TheCommandOutputParms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.lotNo = row.row.lotNo
  Blanking1Parms.value.type = 1
  TheCommandOutputParms.value.lotNo = row.row.lotNo
  TheCommandOutputParms.value.type = 1
  BatchQueryParms.value.lotNo = row.row.lotNo

  TheCommandOutputMethods()
  TransceiverTypeMethods()
  // Blanking1Methods()
  dialogVisible7.value = true
  rowParms.value = row.row
}

// 日期查询
const dateQueryParms = ref({
  dateInfo: ''
})

const Launchform5 = (row: any) => {
  dateQueryParms.value.dateInfo = row[0].dateValue
  dateQueryMethods()
}

const dateQueryTableDate = ref<any>([])

const isLoading5 = ref(false)

const dateQueryMethods = async () => {
  isLoading5.value = true
  try {
    let obj: any = {}
    let obj1: any = {}
    let obj2: any = {}
    let obj4: any = {}

    const result = await dateQuery(dateQueryParms.value.dateInfo)

    for (const key in result) {
      if (key.includes('Lot')) {
        obj[key] = result[key]
      }

      if (key.includes('Match')) {
        obj4.inLotNum = result.inMatchNum
        obj4.outLotNum = result.outMatchNum
      }

      if (key.includes('Part')) {
        obj1.inLotNum = result.inPartNum
        obj1.outLotNum = result.outPartNum
      }

      if (key.includes('Bag')) {
        obj2.inLotNum = result.inBagNum
        obj2.outLotNum = result.outBagNum
      }
    }

    dateQueryTableDate.value = [obj, obj1, obj2, obj4]
    dateQueryTableDate.value[0].title = '指令个数'
    dateQueryTableDate.value[1].title = '部件个数'
    dateQueryTableDate.value[2].title = '袋数'
    dateQueryTableDate.value[3].title = '当日配套数'
    isLoading5.value = false
  } catch (error) {
    JMessage.error('日期查询出错')
    isLoading5.value = false
  }
}

// 删除
const DetailDeletionParms = ref('')

const deleteRow = (row: any) => {
  DetailDeletionParms.value = row.row.id
  handleCloseMateriaLKlc()
}

const handleCloseMateriaLKlc = (_done?: () => void) => {
  ElMessageBox.confirm('确定要删除该数据吗?')
    .then(async () => {
      DetailDeletionMethods()
    })
    .catch(() => {})
}

const DetailDeletionMethods = async () => {
  isLoading1.value = true

  try {
    const result = await DetailDeletion(DetailDeletionParms.value)
    if (result.code === 200) {
      mainQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('删除出错请重试')
  }
  isLoading1.value = false
}

// 部门查询
const deptNameQueryMethods = async () => {
  try {
    let NewArray = []
    const result = await deptNameQuery()
    NewArray = result.reduce((arr: any, item: any) => {
      if (item.deptName) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.deptName + ' ' + item.deptNo)
      }
      return arr
    }, [])
    formList2.value[3].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
  } catch (error) {
    JMessage.error(`缴库单位查询出错`)
  }
}

// 收发类型
const TransceiverTypeParms = ref('')
const TransceiverTypeParms123 = ref<any>([])

const collectType = ref('')

const TransceiverTypeMethods = async () => {
  let NewArray = []
  const result = await TransceiverType(modifTable5String2.value, collectType.value)

  if (result.data !== null) {
    form1.value.remainQty = result[0].count - result[0].qty
    form.value.remainQty = result[0].count - result[0].qty
    NewArray = result.reduce((arr: any, item: any) => {
      if (item.groupNo) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.groupNo + ' ' + item.poNo)
      }
      return arr
    }, [])

    NewArray = result.reduce((arr: any, item: any) => {
      if (item.groupNo) {
        // 只添加非空的 typeName 和 typeNo
        arr.push(item.groupNo)
      }
      return arr
    }, [])
    formList2.value[1].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
    TransceiverTypeParms123.value = EvaluatesTheArrayReturnValue(NewArray)
  } else {
    JMessage.error(result.message)
  }
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

// 冲裁查询
const BlankingParms = ref({
  custom: '',
  deliveryDate: '',
  finish: '',
  line: '',
  lotNo: '',
  pageNum: 1,
  pageSize: 40,
  style: ''
})

const isLoading = ref(true)

const BlankingTableDate = ref([])

const BlankingMethods = async () => {
  isLoading.value = true

  try {
    const result = await Blanking(BlankingParms.value)
    BlankingTableDate.value = result.data.rows.map((item: any) => {
      item.inQty1 = item.orderQty - item.inQty
      item.outQty1 = item.orderQty - item.outQty
      item.sum = item.inQty - item.outQty
      return item
    })

    summaryData.value[0].num = BlankingTableDate.value.reduce((accumulator: any, item: any) => {
      return accumulator + (item.sum || 0) // 确保如果 sum 不存在则使用 0
    }, 0)
    if (result.isVietName) {
      active.value = true
    } else {
      active.value = false
    }
    total.value = result.data.total
    isLoading.value = false
  } catch (error) {
    JMessage.warning('查询异常')
    isLoading.value = false
  }
}

const Launchform3 = (row: any) => {
  BlankingParms.value.lotNo = row[0].inputValue
  BlankingParms.value.custom = row[1].inputValue
  BlankingParms.value.style = row[2].inputValue
  BlankingParms.value.finish = row[3].selectValue
  date.value = row[8].dateValue
}

const total = ref(0)

const handleSizeChange = (val: number) => {
  BlankingParms.value.pageSize = val
  BlankingMethods()
}

const handleCurrentChange = (val: number) => {
  BlankingParms.value.pageNum = val
  BlankingMethods()
}

onMounted(() => {
  BlankingMethods()

  GetPackingMethods()

  const today = new Date()
  const formattedDate = today.toISOString().split('T')[0] // 格式化为 YYYY-MM-DD，例如 2025-10-10
  form.value.collectTime = formattedDate
  form1.value.collectTime = formattedDate
})

const formList1 = ref([
  {
    type: 'dateSingle',
    dateLabel: '日期',
    dateValue: ''
  }
])

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '客户',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '完成状态',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: 0,
        optionValue: 'N',
        optionLabel: '未完成'
      },
      {
        optionKey: 1,
        optionValue: 'Y',
        optionLabel: '已完成'
      }
    ]
  },
  {
    type: 'button',
    checkboxLabel: '搜索'
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  },
  {
    type: 'radio',
    checkboxLabel: '出库',
    checked: false,
    label: '2'
  },

  {
    type: 'radio',
    checkboxLabel: '缴库',
    checked: false,
    label: '1'
  },
  {
    type: 'dateSingle',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
const number = ref(1)
const date = ref('')
const clickNumber1 = (row: any) => {
  number.value = row
}
const findData = async (row: any) => {
  if (row === '导出') {
    const res = (await fileRequest.get(`/stockWork/report/stockIn/invoices?date=${date.value}&type=${number.value}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (row === '搜索') {
    BlankingMethods()
  }
  if (row === '置空') {
    clearFormList()
  }
}

// 冲裁查询
const mainQueryParms = ref({
  collectType: '',
  groupNo: '',
  line: '',
  lotNo: '',
  pageNum: 1,
  pageSize: 40,
  poNo: ''
})

const dialogVisible6 = ref(false)

const Launchform = (row: any) => {
  mainQueryParms.value.collectType = row[0].selectValue
  mainQueryParms.value.groupNo = row[1].selectValue.split(' ')[0]
  mainQueryParms.value.poNo = row[2].inputValue
  mainQueryParms.value.line = row[3].selectValue.split(' ')[0]

  mainQueryMethods()
}

const total1 = ref(0)

const handleSizeChange1 = (val: number) => {
  mainQueryParms.value.pageSize = val
  mainQueryMethods()
}

const handleCurrentChange1 = (val: number) => {
  mainQueryParms.value.pageNum = val
  mainQueryMethods()
}
const rowParms = ref()
// 收发明细
const modifTable3String = ref('')
const modifTable3 = (row: any) => {
  rowParms.value = row.row
  mainQueryParms.value.lotNo = row.row.lotNo
  dialogVisible6.value = true
  modifTable3String.value = `指令【${row.row.lotNo}】收发明细`

  mainQueryMethods()
  TransceiverTypeMethods()
  deptNameQueryMethods()
}

const formList2 = <any>ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '操作类型',
    selectLabel: '',
    selectOptions: [
      {
        optionKey: 0,
        optionValue: '',
        optionLabel: '所有'
      },
      {
        optionKey: 1,
        optionValue: 'I',
        optionLabel: '缴库'
      },
      {
        optionKey: 2,
        optionValue: 'O',
        optionLabel: '发货'
      }
    ]
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '配码',
    selectLabel: '',
    selectOptions: []
  },

  {
    type: 'input',
    inputPlaceholder: 'PO',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '缴库单位',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList5 = ref([
  {
    type: 'select1',
    selectValue: '',
    selectP: '号码',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select1',
    selectValue: '',
    selectP: '轮次',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const findData1 = (row: string) => {
  if (row === '置空') {
    formList5.value.forEach((item: any) => {
      item.selectValue1 = null
    })
  }
}

const mainQueryTableDate = ref([])

const isLoading1 = ref(false)

const mainQueryMethods = async () => {
  isLoading1.value = true
  try {
    const result = await mainQuery(mainQueryParms.value)
    console.log(result)

    mainQueryTableDate.value = result.data.rows.map((item: any) => {
      if (item.collectType === 'I') {
        item.collectType = '缴库'
      } else {
        item.collectType = '出货'
      }

      return item
    })
    total1.value = result.data.total
    isLoading1.value = false
  } catch (error) {
    isLoading1.value = false
    JMessage.error('查询出错')
  }
}
// 表格数据
const tableHeader = ref([
  {
    label: '品牌',
    prop: 'custom'
  },
  {
    label: '指令号',
    prop: 'lotNo'
  },
  {
    label: '工厂型体',
    prop: 'style'
  },
  {
    label: '订单量',
    prop: 'orderQty'
  },
  {
    label: '线别',
    prop: 'line'
  },

  {
    label: '入库量',
    prop: 'inQty'
  },
  {
    label: '入库欠数',
    prop: 'inQty1'
  },
  {
    label: '出库量',
    prop: 'outQty'
  },

  {
    label: '出库欠数',
    prop: 'outQty1'
  },
  {
    label: '库存量',
    prop: 'sum'
  },

  {
    label: '客人交期',
    prop: 'deliveryDate'
  },
  {
    label: '最近操作时间',
    prop: 'sysDate'
  }
])

const tableHeader1 = ref([
  {
    label: '类型',
    prop: 'collectType'
  },
  {
    label: '海关编码',
    prop: 'customsCode'
  },
  {
    label: '收发日期',
    prop: 'collectTime'
  },
  {
    label: '缴库单位',
    prop: 'line'
  },

  {
    label: '配码',
    prop: 'groupNo'
  },
  {
    label: 'PO',
    prop: 'poNo'
  },
  {
    label: '装箱方式',
    prop: 'packType'
  },
  {
    label: '箱数',
    prop: 'caseQty'
  },
  {
    label: '双数',
    prop: 'pairQty'
  },

  {
    label: '货柜号',
    prop: 'containerNo'
  },
  {
    label: '发票号',
    prop: 'invoiceNo'
  },
  {
    label: '操作员',
    prop: 'sysUser'
  },
  {
    label: '操作时间',
    prop: 'sysDate'
  }
])

const tableHeader2 = ref([
  {
    label: '装箱方式',
    prop: 'title',
    type: 'select'
  },
  {
    label: '箱数',
    prop: 'inLotNum'
  },
  {
    label: '双数',
    prop: 'outLotNum',
    change: true
  }
])

const tableHeader23 = ref([
  {
    label: '装箱方式',
    prop: 'title',
    type: 'select'
  },
  {
    label: '箱数',
    prop: 'inLotNum'
  },
  {
    label: '双数',
    prop: 'outLotNum',
    change: true
  }
])

const tableHeader3 = ref([
  {
    label: '类型',
    prop: 'partName'
  }
])

const tableHeader5 = ref([
  {
    label: '部位',
    prop: 'partName',
    change: true
  },
  {
    label: '号码',
    prop: 'sizeNo',
    change: true
  },
  {
    label: '轮次',
    prop: 'lsNo',
    change: true
  },
  {
    label: '剩余数量',
    prop: 'inQty',
    change: true
  },
  {
    label: '报数',
    prop: 'inQty1'
  }
])

const tableHeader6 = ref([
  {
    label: '批次',
    prop: 'batchNo',
    change: true
  },
  {
    label: '最少收/发数量',
    prop: 'qty',
    change: true
  }
])
</script>

<style lang="less" scoped></style>
